<!-- 原料管理 -->
<div class="inner wrp wrp_data_box" id="table_item">
    <div style="display:none;" class="page_global_tips" id="binding_limit_hint"></div>
    <div class="data_box_hd js_all" id="js_mobile">
        <div id="item_tab_nav" class="label_tab">
            <a href="javascript:void(0);" data-url="{% url "smErp:create_material" %}" class="btn js_create jsUrlLink btn_disabled" target="_self">
                <span>创建原料</span>
            </a>
            <div class="bubble_tips bubble_left">
                <div class="bubble_tips_inner">
                    <p class="mass_send_tips" id="tips1">
                        <span>还可创建0个原料</span>
                    </p>
                </div>
                <i class="bubble_tips_arrow out"></i>
                <i class="bubble_tips_arrow in"></i>
            </div>
        </div>
    </div>

    <div class="data_box_bd js_all">
        <div class="table_wrp app_list">
            <!-- 原料列表 -->
            <table class="table mp_account" id="material_tb" cellspacing="0">
                <thead class="thead">
                <tr>
                    <th class="table_cell content">原料名称</th>
                    <th class="table_cell content">产地</th>
                    <th class="table_cell content">来源</th>
                    <th class="table_cell content">资料地址</th>
                    <th class="table_cell content">参考图</th>
                    <th class="table_cell content">创建时间</th>
                </tr>
                </thead>
                <tbody class="tbody" id="material_table_bd">
                <tr>
                    <td colspan="6">
                        <p class="empty_tips">暂无数据</p>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
{% block footscript %}
    <script>
        function getCookie(c_name)
        {
            if (document.cookie.length>0)
            {
                c_start=document.cookie.indexOf(c_name + "=");
                if (c_start!=-1)
                {
                    c_start=c_start + c_name.length+1;
                    c_end=document.cookie.indexOf(";",c_start);
                    if (c_end==-1) c_end=document.cookie.length;
                    return unescape(document.cookie.substring(c_start,c_end))
                }
            }
            return ""
        }
        function show_material_list(){
            // 通过接口获取原料列表
            $.ajax({
                type: 'POST',
                url: '{% url "smErp:manage_material" %}',
                headers:{ "X-CSRFtoken":getCookie("csrftoken")},
                dataType: 'json',
                success: function(data){
                    if (data.result == 0) {
                        console.log(data);
                        // 成功显示成功
                        if(data.can_material_nums > 0){
                            $("#item_tab_nav").children("a").attr("href", '{% url "smErp:create_material" %}').attr("data-url", '{% url "smErp:create_material" %}');
                            $("#item_tab_nav").children("a").removeClass("btn_disabled").addClass("btn_primary");
                        }else{
                            $("#item_tab_nav").children("a").attr("href", 'javascript:;').attr("data-url", '');
                            $("#item_tab_nav").children("a").removeClass("btn_primary").addClass("btn_disabled");
                        }
                        if(data.material_list.length > 0){
                            // 清空表体
                            $("#material_table_bd").empty();
                            var material_list = data.material_list ;
                            for(i = 0 ; i < material_list.length; i++){
                                var html = '<tr><td colspan="1"><p data-name="name" data-pk="'+ material_list[i].id + '" class="table_cell content material_change">' + material_list[i].name + '</p></td>'+
                                        '<td colspan="1"><p data-name="producer" data-pk="'+ material_list[i].id + '" class="table_cell content material_change">' + material_list[i].producer + '</p></td>'+
                                        '<td colspan="1"><p data-name="source_type" data-pk="'+ material_list[i].id + '" class="table_cell content material_change">' + material_list[i].source_type + '</p></td>'+
                                        '<td colspan="1"><p data-name="ref_url" data-pk="'+ material_list[i].id + '" class="table_cell content material_change">' + material_list[i].ref_url + '</p></td>'+
                                        '<td colspan="1"><p data-name="ref_image" data-pk="'+ material_list[i].id + '" class="table_cell content"><img class="thumbnail_image" src="' + material_list[i].ref_image + '" onclick="showBoxImage(this)"/></p></td>'+
                                        '<td colspan="1"><p class="table_cell content">'+ material_list[i].create_time + '</p></td></tr>';
                                $("#material_table_bd").append(html);
                            }
                            $("#material_table_bd").append('<tr><td colspan="6"><p class="empty_tips"></p></td></tr>');
                        }
                        $("#item_tab_nav").children("a").children("span").html("创建原料");
                        $("#tips1").children("span").html("还可创建" + data.can_material_nums + "种原料");

                    } else {
                        //提示层
                        layer.msg(data.reason);
                    }
                }
            });
        }
        show_material_list();

        // 原料管理
        $("#material_table_bd").on("click", '.material_change', function(){
            $(this).editable({
                type : 'text',
                pk : $(this).data("pk"),
                params: function(params){
                    params.name = $(this).data("name");
                    return params;
                },
                placeholder: '请输入原料名称',
                url : function(params) {
                    $.ajax({
                        url: "{% url 'smErp:material_change' %}",
                        type:"POST",
                        data: params,
                        headers:{ "X-CSRFtoken":getCookie("csrftoken")},
                        success:function (data) {
                            console.log(data);
                            if(data.result == 0){
                                // 将表格此项删除
                                layer.msg("修改成功");
                            }else{
                                layer.msg(data.reason);
                            }
                        }
                    });
                },
                validate : function(value) {
                    if (value == '') {
                        return '不能为空';
                    }
                }
            });
        });
    </script>
{% endblock %}




